package com.sharemedia.managers.db
{
	import com.sharemedia.interfaces.IDBCommand;
	
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.events.Event;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;

	[Event(name="complete", type="flash.events.Event")]
	public class AddTracksCommand implements IDBCommand
	{
		private const ADD_TRACKS:String = "INSERT INTO tracks (uid, fileName, path) VALUES (@uid, @fileName, @path)";
		
		private var tracks:Array;
		public function AddTracksCommand(tracks:Array)
		{
			super();
			this.tracks = tracks;
		}
		
		public function execute(connection:SQLConnection):void
		{
			var addTracksStat:SQLStatement = new SQLStatement();
			addTracksStat.sqlConnection = connection;
			addTracksStat.text = ADD_TRACKS;
			
			addTracksStat.addEventListener(SQLErrorEvent.ERROR, errorHandler);
			
			for (var i:int = 0; i < tracks.length; i++) 
			{
				addTracksStat.parameters["@uid"] = tracks[i].uid;	
				addTracksStat.parameters["@fileName"] = tracks[i].fileName;	
				addTracksStat.parameters["@path"] = tracks[i].path;	
				
				if( i == tracks.length-1)
				{
					addTracksStat.addEventListener(SQLEvent.RESULT, insertResultHandler);
				}
				
				addTracksStat.execute();
			}
		}
		
		/**
		 */
		private function errorHandler(e:SQLErrorEvent):void
		{
		}
		
		protected function insertResultHandler(event:Event):void
		{
		}
	}
}